function getParam(paramName) {
    paramValue = "";
    isFound = false;
    if (this.location.search.indexOf("?") == 0 && this.location.search.indexOf("=") > 1) {
        arrSource = decodeURI(this.location.search).substring(1, this.location.search.length).split("&");

        i = 0;
        while (i < arrSource.length && !isFound) {
            if (arrSource[i].indexOf("=") > 0) {
                if (arrSource[i].split("=")[0].toLowerCase() == paramName.toLowerCase()) {
                    paramValue = arrSource[i].split("=")[1];
                    isFound = true;
                }
            }
            i++;
        }
    }
    return paramValue;
}
var serialNumber = hardware.STB.serialNumber;
var caserialNumber = CA.card.serialNumber;
var serialNumber = 666;
var caserialNumber = 888;
EVM.cookie.setValue('serialNumber', serialNumber)
EVM.cookie.setValue('caserialNumber', caserialNumber)
var loginStatus = '';
var urladdress = getParam("urladdress");
// 约定：如果url是“portal”，则跳到固定页。如果url为空，则返回上一页。如果url不是“portal”，则跳到url的页面
function gobackurl(urladdress) {
    if ((urladdress.toLowerCase()) == "portal") {
        setTimeout(function () {
            if (typeof (Android) != "undefined") { // 安卓盒子
                iPanel.eventFrame.openPortal();
            } else {
                window.location.href = "http://10.65.255.6:8080/portalrouting/pr.do?stbid=" + hardware.STB.serialNumber;
            }
        }, 2000)

    } else if (urladdress == "" || urladdress == null) {
        setTimeout(function () {
            history.go(-1);
        }, 2000)

    } else {
        setTimeout(function () {
            window.location.href = urladdress
        }, 2000)
    }
}
// function getReturnUrl() {

//     window.location.href = "http://10.2.11.143/ncov2019/index.htm"

// //   var ajax;
// //   if (window.XMLHttpRequest) {
// //     ajax = new XMLHttpRequest();
// //   } 
// //   else {
// //     ajax = new ActiveXObject('Microsoft.XMLHTTP');
// //   }
// //   ajax.open('post', STATICURL + "activity/getbackurl", true);
// //   ajax.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
// //   ajax.send();
// //   ajax.onreadystatechange = function () {
// //     if (ajax.readyState == 4 && ajax.status == 200) {
// //       // debugger
// //       var text = ajax.responseText;
// //       // if (text.length != 0) {

// //       //   var response = eval("(" + text + ")");
// //       //   // url = response
// //       // }
// //       console.log(text);
// //       // url = text;
// //       setTimeout(function() {
// //         window.location.href = text
// //       },500)      
// //     }
// //   }
// }
function doBack() {

    if (typeof (Android) != "undefined") {
        //tvos版本
        iPanel.eventFrame.openPortal();
    } else {
        gobackurl(urladdress)
        //中间件版本
        //   getReturnUrl()
        //  window.location.href = "http://10.65.255.6:8080/portalrouting/pr.do?stbid=" + hardware.STB.serialNumber;
    }

}

function modelInit() {
    //面包屑

    if (modelIndex.crumbs) {
        TV.structure(modelIndex.crumbs)
    }
    $.cookie('TV_CST_userName', '小王', {
        expires: 7
    });
    var indexList = new TV.CookieOpt().getOpt()
    // console.log(modelIndex.disHeader, 'modelIndex.disHeader')
    var index = indexList && typeof indexList.index == 'number' ? indexList.index : undefined;
    if (!modelIndex.disUser) {
        // loginStatus = TV.UserMess()
    }
    right.toggleFocus(index);
}
/*
 * 1.右侧区
 */

cmsIndex.modules[cmsIndex.moduleGuid(cmsIndex.addModule())] = {
    modName: 'right',
    modNum: cmsIndex.moduleGuid(),
    map: {
        left: 0
    }
};

var right = cmsIndex.modules[cmsIndex.moduleGuid()].module = (function () {
    var mod = {
            modName: "index",
            modNum: cmsIndex.moduleGuid()
        },
        index = modelIndex.index, //位置
        listName = modelIndex.listName, //模块名
        containerId = modelIndex.containerId; //包含模块的标签id

    //操作
    mod.toggleFocus = function (number) {
        var me = mod;
        if (number != undefined) {
            index = number;
        }
        TV.IndexOpt(containerId, listName, index);
        if (index == 0) {
            document.getElementById('pre').style.backgroundImage = "url('./images/enterQuestion_active.png')";
            document.getElementById('next').style.backgroundImage = "url('./images/record.png')";
            document.getElementById('back').style.backgroundImage = "url('./images/exit.png')";

            document.getElementById('model1').style.backgroundImage = "url('./images/title_bg_active.png')";
            document.getElementById('model2').style.backgroundImage = "url('./images/title2.png')";
            document.getElementById('model3').style.backgroundImage = "url('./images/title2.png')";
            document.getElementById('model4').style.backgroundImage = "url('./images/tt_back.png')";
        } else if (index == 1) {
            document.getElementById('pre').style.backgroundImage = "url('./images/enterQuestion.png')";
            document.getElementById('next').style.backgroundImage = "url('./images/record_active.png')";
            document.getElementById('back').style.backgroundImage = "url('./images/exit.png')";

            document.getElementById('model1').style.backgroundImage = "url('./images/title2.png')";
            document.getElementById('model2').style.backgroundImage = "url('./images/title_bg_active.png')";
            document.getElementById('model3').style.backgroundImage = "url('./images/title2.png')";
            document.getElementById('model4').style.backgroundImage = "url('./images/tt_back.png')";
        } else if (index == 2) {
            document.getElementById('pre').style.backgroundImage = "url('./images/enterQuestion.png')";
            document.getElementById('next').style.backgroundImage = "url('./images/record.png')";
            document.getElementById('back').style.backgroundImage = "url('./images/exit_active.png')";
            document.getElementById('model1').style.backgroundImage = "url('./images/title2.png')";
            document.getElementById('model2').style.backgroundImage = "url('./images/title2.png')";
            document.getElementById('model3').style.backgroundImage = "url('./images/title_bg_active.png')";
            document.getElementById('model4').style.backgroundImage = "url('./images/tt_back.png')";
        } else if (index == 3) {
            document.getElementById('model1').style.backgroundImage = "url('./images/title2.png')";
            document.getElementById('model2').style.backgroundImage = "url('./images/title2.png')";
            document.getElementById('model3').style.backgroundImage = "url('./images/title2.png')";
            document.getElementById('model4').style.backgroundImage = "url('./images/tt_back_active.png')";
        }
        return me;
    };

    //index 位置调整
    mod.moveOp = function (directive) {
        var me = mod;
        index = direOpt(index, directive);
        mod.toggleFocus();
        return me;
    };
    mod.action = function (directive) {
        var me = mod;
        switch (directive) {
            case 'back':
            case 640:
            case 340:
            case 32:
            case 8:
                evt.preventDefault(); //阻止浏览器默认事件
                evt.stopPropagation(); //冒泡事件
                // doBack();
                gobackurl(urladdress)
                // getReturnUrl();
                // return 0;
                // if (modelIndex.isVideo && mp) {
                //     mp.stop();
                // }
                // if (modelIndex.isIndex) {
                //     window.close();
                // } else {
                //     history.go(-1);
                // }

                // break;
            case 'enter':
                if (modelIndex.isVideo && mp) {
                    mp.stop();
                }
                if (isCache) {
                    var cookieTv = new TV.CookieOpt({
                        index: index
                    })
                    var indexList = cookieTv.setOpt();
                }
                if (index == -1) {
                    if (loginStatus) {
                        if (modelIndex.isIndex) {
                            window.location.href = './pages/user.html'
                        } else {
                            window.location.href = '../pages/user.html'
                        }
                    } else {
                        if (modelIndex.isIndex) {
                            window.location.href = './pages/login.html'
                        } else {
                            window.location.href = '../pages/login.html'
                        }
                    }

                } else if (index >= 0) {

                    direEnter(index);

                }
                break;
        }
        return me;
    };
    mod.grabEvent = function (key) {
        var me = mod;
        var keyJson = TV.GetKey(key);
        if (keyJson == 0) {
            gobackurl(urladdress)
            // getReturnUrl();
        } else {
            if (keyJson.keyState && keyJson.keyState != 'enter' && keyJson.keyState != 'back') {
                me.moveOp(keyJson.keyState);
            } else {
                me.action(keyJson.keyState)
            }
            return keyJson.isBlock;
        }



    };

    return mod;
})();

cmsIndex.run();